/**
 * 
 */
package cn.nawang.ebeim.ui.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;

import cn.nawang.ebeim.client.util.PathUtil;
import cn.nawang.ebeim.client.util.SystemUtil;

/**
 * @ClassName: PropertiesUtil
 * @Description: 读取配置文件工具类
 * @author 张建峰
 * @date 2015年8月20日 下午2:03:03
 * @version v1.0
 */
public class PropertiesUtil {
  
  public static void main(String[] args) throws Exception {
    System.out.println(readAll());
//    System.out.println(PropertiesUtil.getConfPath());
//    System.out.println(read("working.path"));
  }
  
  
  // 配置文件路径
  public static final String PROPERTIES_FILE_PATH = "/resources/config.properties";

  public static String read(String key) {
    Properties props = new Properties();
    InputStream in = null;
    try { 
      // 从目录“程序目录/conf”下加载配置文件
      File rootDir = new File(PathUtil.getClassPath());
      String confPath =  rootDir+"/others/conf/config.properties";//生成exe的时候要放开，调试的时候注释掉 
//      String confPath =  rootDir+"/conf/config.properties";
      confPath = java.net.URLDecoder.decode(confPath,"utf-8");//读取文件进行路径编码，要不然路径中有中文会乱码、特殊字符会转义
      in = new FileInputStream(new File(confPath));
      props.load(in);
      String value = props.getProperty(key);
      in.close();
      return value;
    } catch (Exception e) {
      e.printStackTrace();
      if (in != null) {
        try {
          in.close();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
      return null;
    }
  }

  public static Map<String, String> readAll() throws Exception {
    Properties props = new Properties();
    Map<String, String> map = null;
    String confPath = SystemUtil.getConfPath(new PropertiesUtil()) + "/config.properties"; // 从目录“程序目录/conf”下加载配置文件
    System.out.println(confPath);
    InputStream in = new FileInputStream(new File(confPath));
    props.load(in);
    Enumeration en = props.propertyNames();
    map = new HashMap<String, String>();
    while (en.hasMoreElements()) {
      String key = (String) en.nextElement();
      String Property = props.getProperty(key);
      map.put(key, Property);
    }
    return map;
  }

}
